//app.js
App({
  token: '',
  env  : 'test',
  getApiDomain: function(){
    var domain = 'http://wxc.hejinmin.cn/apiv1/';
    if(this.evn=='test'){
      domain = 'http://wxc.hejinmin.cn/apiv1/';
    }
    return domain;
  },
  http: function(o){  //http请求方法
    wx.showLoading({
      title: '正在处理',
    })
    var that = this;
    var data = o.data || {};
    data.token = that.token;
    console.log(that.token);
    data.platform = 'wechat_app';
    var header = o.header || {};
    var method = o.method || 'POST';
    wx.request({
      url: that.getApiDomain()+o.url,
      data: data,
      header : header,
      method : method,
      success: function(res){
        if(res.data.err){
          that.alert(res.data.msg);
          return false;
        }
        typeof o.success == 'function' && o.success(res.data);
      },
      fail: function(res){
        wx.showToast({
          title: '网络不可用',
          icon : 'loading'
        });
      },
      complete:function(res){
        wx.hideLoading();
      }
    });
  },
  alert: function(content,title){
    var title   = title   || '温馨提醒';
    var content = content || '未知错误';
    wx.showModal({
      title: title,
      content: content,
      showCancel: false
    })
  },
  onLaunch: function () {
    //调用API从本地缓存中获取数据
  },
  getUserInfo:function(cb){
    var that = this;
    var userInfo = '';
    userInfo = wx.getStorageSync('wms_userInfo');

    if (userInfo){
      return userInfo;
    }

    wx.login({
      success : function(res){
        that.http({
          url : 'home/wxusercode',
          data: res,
          success: function(res){
            if(res.err){
              that.alert(res.msg);
              return false;
            }
            that.token = res.data.session_3rd;
            //获取用户信息
            wx.getUserInfo({
              success(res){
                that.http({
                  url: 'home/userlogin',
                  data:res,
                  success: function(res){

                  }
                });
              }
            })
          }
        });
      },
      fail: function(res){
        console.log(res);
      }
    });
  },
  globalData:{
    userInfo:null
  }
})